package org.zjor.lwuit.ext.menu;

import com.sun.lwuit.Component;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.list.ListCellRenderer;

/**
 *
 * @author roiz
 */
public class CenteredListCellRenderer implements ListCellRenderer{

    private Label item = new Label("");
    private Label focus = new Label("");

    private int marginDivisor = 2;
    private int orientation = Component.LEFT;

    private int margin;
    private int maxItemWidth;
    private int maxWidth;

    public Component getListCellRendererComponent(List list, Object o, int i, boolean bln) {

        MenuItem model = (MenuItem)o;
        
        item.setIcon(model.getImage());
        item.setText(model.getText());


        if (list.getWidth() > maxWidth) {
            maxWidth = list.getWidth();
        }

        if (item.getPreferredW() > maxItemWidth) {
            maxItemWidth = item.getPreferredW();
        }
        
        margin = (maxWidth-maxItemWidth)/marginDivisor;
        margin = margin>0?margin:0;

        item.getStyle().setPadding(orientation, margin);
        return item;
    }

    public Component getListFocusComponent(List list) {
        return focus;
    }

    public Label getFocusLabel() {
        return focus;
    }

    public Label getItemLabel() {
        return item;
    }

    public int getMarginDivisor() {
        return marginDivisor;
    }

    public void setMarginDivisor(int marginDivisor) {
        this.marginDivisor = marginDivisor;
    }

    public int getOrientation() {
        return orientation;
    }

    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }
    

}
